<?php
namespace app\shop\model;
use app\BaseModel;
use think\facade\Db;


class IndexGoods extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_goods';

    protected $append = [
        'collage_text',
        'price_text',
        'original_price'
    ];


    /**
     * @param $value
     * @param $data
     * @return mixed
     * 获取拼团信息
     */
    public function getCollageTextAttr($value,$data){

        if(isset($data['id'])&&isset($data['uniacid'])&&isset($data['freight'])){

            $collage_model  = new IndexShopCollage();

            $dis['goods_id']= $data['id'];

            $dis['uniacid'] = $data['uniacid'];

            $dis['status']  = 1;

            $list = $collage_model->collageList($dis);

            if(!empty($list)){

                foreach ($list as &$value){

                    $value['freight'] = $data['freight'];
                }
            }
            return $list;
        }

    }

    /**
     * User: chenniang
     * Date: 2019-09-27 16:02
     * @param $value
     * @param $data
     * @return void
     * descption:获取总库存
     */
    public function getStockAttr($value,$data){

        if(!empty($data['id'])){

            return Db::name('longbing_card_shop_spe_price')->where(['goods_id'=>$data['id'],'status'=>1])->sum('stock');
        }

    }
    /**
     * User: chenniang
     * Date: 2019-09-23 15:24
     * @return void
     * descption:获取规格的第一个价格
     */
    public function getPriceTextAttr($value,$data){

        $spe_price = new AdminShopSpePrice();

        return $spe_price->where(['status'=>1,'goods_id'=>$data['id']])->min('price');

    }
    /**
     * User: chenniang
     * Date: 2019-11-05 10:48
     * @return void
     * descption:获取规格的最低原价
     */
    public function getOriginalPriceAttr($value,$data){

        $o_dis[] = ['status','=',1];

        $o_dis[] = ['goods_id','=',$data['id']];

        $o_dis[] = ['original_price','<>',0];

        $original_price = Db::name('longbing_card_shop_spe_price')->where($o_dis)->min('original_price');

        return $original_price;
    }

    /**
     * User: chenniang
     * Date: 2019-09-28 14:47
     * @return void
     * descption:转换富文本
     */
    public function getContentAttr($value){

        return str_replace("&amp;nbsp;","",getimgs($value));

    }
    /**
     * 获取商品列表
     */
    public function goodList($dis,$uid,$page=10,$where,$where2,$company_id){
        //判断查询条件中是否有a.id
        $is_id = self::inArray($dis,'a.id');

        if(!$is_id){
            //查询公司绑定的商品id
            $company_goods = new AdminCompanyGoods();

            $goods_ids = $company_goods->getCompanyGoodsIds($where);
            //查询公共的商品id
            $public_goods_ids = $this->where($where2)->column('id');
            //商品id拼接
            $new_goods_ids = explode( ',',implode($goods_ids,',').','.implode($public_goods_ids,','));
            //添加新的查询条件
            $dis[] = ['a.id','in',$new_goods_ids];
        }


        $data = $this
            ->alias('a')
//            ->leftJoin('longbing_card_company_goods b' ,'a.id = b.goods_id')
            ->leftJoin('longbing_card_extension c' ,'a.id = c.goods_id AND c.status = 1 AND c.user_id ='.$uid)
            ->where($dis)
//            ->field(['a.*,b.company_id,ifnull(c.status,0) as is_extension'])
            ->field(['a.*,ifnull(c.status,0) as is_extension'])
            ->group('a.id')
            ->order('c.status desc,a.recommend desc,a.top desc,a.update_time desc')
            ->paginate($page)
            ->toArray();


        $data['data'] = formatNumberPrice($data['data'],['price']);
        $data['data'] = transImages($data['data'],['cover','images'],',');
        if(!empty($data['data'])){
            foreach ($data['data'] as &$v){
                $v['is_collage'] = empty($v['collage_text'])?0:1;
                $v['company_id'] = $company_id;
            }
        }
        return $data;
    }


    /**
     * @param $dis
     * @param $uid
     * @param int $page
     * @param $where
     * @param $where2
     * @param $company_id
     * @功能说明:商品列表
     * @author chenniang
     * @DataTime: 2020-07-29 17:53
     */
    public function goodListV2($dis,$uid,$page=10,$where){

        $data = $this->alias('a')
                ->leftJoin('longbing_card_company_goods b' ,'a.id = b.goods_id')
                ->leftJoin('longbing_card_extension c' ,'a.id = c.goods_id AND c.status = 1 AND c.user_id ='.$uid)
                ->where($dis)
                ->where(function ($query) use ($where){
                    $query->whereOr($where);
                })
                ->field(['a.*,b.company_id,ifnull(c.status,0) as is_extension'])
                ->group('a.id')
                ->order('c.status desc,a.recommend desc,a.top desc,a.id desc,a.update_time desc')
                ->paginate($page)
                ->toArray();

        $data['data'] = formatNumberPrice($data['data'],['price']);

        $data['data'] = transImages($data['data'],['cover','images'],',');


        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['is_collage'] = empty($v['collage_text'])?0:1;
            }
        }
        return $data;
    }


    /**
     * @param $dis
     * @param $where
     * @param int $page
     * @功能说明:获取公司下面的商品
     * @author chenniang
     * @DataTime: 2022-01-20 14:14
     */
    public function companyGoodsList($dis,$where,$page=10){

        $data = $this->alias('a')
            ->leftJoin('longbing_card_company_goods b' ,'a.id = b.goods_id')
            ->where($dis)
            ->where(function ($query) use ($where){
                $query->whereOr($where);
            })
            ->field(['a.*,b.company_id'])
            ->group('a.id')
            ->order('a.recommend desc,a.top desc,a.id desc,a.update_time desc')
            ->paginate($page)
            ->toArray();

        $data['data'] = formatNumberPrice($data['data'],['price']);

        $data['data'] = transImages($data['data'],['cover','images'],',');

        return $data;
    }

    /**
     * @param $dis
     * @param $str
     * @功能说明:判断二维数组是否存在某个键值
     * @author lxj
     * @DataTime: 2020/5/14 10:34
     */
    public function inArray($dis,$str){
        foreach ($dis as $key =>$val){
            if(in_array($str,$val)){
                return true;
            }
        }
        return false;
    }
    /**
     * @param $dis
     * 获取商品详情
     */
    public function goodsInfo($dis){
        $data = self::where($dis)->find();
        if(!empty($data)){
            $spe_price = new AdminShopSpePrice();
            $o_dis[]   = ['status','=',1];
            $o_dis[]   = ['goods_id','=',$data->id];
            $max_price = $spe_price->where($o_dis)->max('price');
            $data->area_price = $data->price_text !=$max_price? $data->price_text.'~'.$max_price:$data->price_text;
            $o_dis[]   = ['original_price','<>',0];
            $max_original_price = $spe_price->where($o_dis)->max('original_price');
            $data->area_original_price =!empty($max_original_price)?$data->original_price.'~'.$max_original_price:0;
            if($data->area_original_price!=0){
                $data->area_original_price = $max_original_price != $data->original_price?$data->area_original_price:$max_original_price;
            }
        }
        return !empty($data)?$data->toArray():[];
    }

    /**
     * @param $dis
     * @param $data
     * @return IndexShopSpePrice
     * 编辑商品
     */

    public function goodsUpdate($dis,$data){
        $data['update_time'] = time();
        $res = $this->where($dis)->update($data);
        return $res;

    }
    /**
     * User: chenniang
     * Date: 2019-09-29 19:16
     * @param $dis
     * @param $sort
     * @return array
     * descption:我的分销商品查询
     */
    public function sellingGoods($dis,$sort,$where=[]){
        $data = $this
            ->alias('a')
            ->leftJoin('longbing_card_shop_spe_price b','b.goods_id = a.id AND b.status = 1')
            ->leftJoin('longbing_card_company_goods c' ,'a.id = c.goods_id')
            ->where($dis)
            ->where(function ($query) use ($where){
                $query->whereOr($where);
            })
            ->field('a.*,min(b.price) as min_price')
            ->group('a.id')
            ->order($sort)
            ->paginate(10)
            ->toArray();
        return $data;
    }









}